<?php

class Table extends HTMLElement {
	private $rows = array();
	private $cellCssStyle;
	private $firstRowClass;
	
	public function __construct($id = null)
	{
		$this->setId($id);
	}
	
	/**
	 * @return unknown
	 */
	public function getRows() {
		return $this->rows;
	}
	
	/**
	 * @param array $rows
	 */
	public function setRows($data) {
		if(0 != count($data) && 0 != count($data[0]))
		{
			$this->rows = $data;
		}
	}
	
	public function addRow(array $row)
	{
			$this->rows[] = $row;
	}
	
	public function hasRows()
	{
		return (bool)(null != $this->rows);
	}
	
	public function setCellCssStyle(CssStyle $style)
	{
		$this->cellCssStyle = $style;
	}
	public function hasCellCssStyle()
	{
		return (bool)(null !=$this->cellCssStyle);
	}
	
	public function setFirstRowClass($class)
	{
		$this->firstRowClass = $class;
	}
	public function hasFirstRowClass()
	{
		return (bool)(null != $this->firstRowClass);
	}
	
	
	/**
	 * 
	 * @see HTMLElement::getHTML()
	 */
	public function getHTML() {
		$html = "\n";
		$html .= '<table'; 		
		$html = $this->addAttributes($html);
		$html .= '>';
		
		$tdOpenString = '<td';		
		if(true == $this->hasCellCssStyle())
		{
			$tdOpenString .= ' '.$this->getCssStyle()->getStyleString();
		}
		$tdOpenString .='>';
		
		for($i = 0; $i < count($this->rows); $i++)
		{
			$row = $this->rows[$i];
			$html .= "\n\t";
			$html .= '<tr';	
			if($i == 0 && $this->hasFirstRowClass())
			{
				$html .= ' class="'.$this->firstRowClass.'"';
			}
			$html .='>';
			
			foreach ($row as $value)
			{
				
				$html .= "\n\t\t";
				$html .= $tdOpenString;
				$html .= $value;
				$html .= '</td>';
			}
			$html .= "\n\t";
			$html .= '</tr>';
		}
		$html .= "\n";
		$html .= '</table>';
		return $html;
	}
}

?>